package com.study;

import com.study.entity.User;
import com.study.mapper.UserMapper;
import com.study.tool.JsonTool;

import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

public class StreamTest01 {
    public static List<User> list = new ArrayList<>();
    public static Map<String, User> map = new HashMap<>();

    public static void main(String[] args) {

        //定义三个用户对象
        User user1 = new User();
        user1.setUsername("tom");
        user1.setPassword("1");
        User user2 = new User();
        user2.setUsername("jerry");
        user2.setPassword("06");
        User user3 = new User();
        user3.setUsername("mike");
        user3.setPassword("3");

        //添加用户到集合中
        list.add(user1);
        list.add(user2);
        list.add(user3);

        map.put(user1.getUsername(), user1);
        map.put(user2.getUsername(), user2);
        map.put(user3.getUsername(), user3);

        AtomicBoolean flag = new AtomicBoolean(false);//默认不算

        //在集合中查询用户名为huxiansen的集合
        List<User> userList = list.stream().filter(user -> "tom".equals(user.getUsername())).collect(Collectors.toList());
        System.out.println("userList01: " + JsonTool.toJson(userList));
//        //在集合中查询出第一个用户密码为123456的用户
        Optional<User> user = list.stream().filter(userTemp -> "2".equals(userTemp.getPassword())).findFirst();
        System.out.println("userList02: " + JsonTool.toJson(user));

        // 纯0必算,纯1不算,既有0又有1必算
        list.stream().filter(user4 -> {
            User user5 = UserMapper.selectById(user4.getUsername());
            if (null == user5) {
                return user5.getUsername().substring(0, 2).contains("06");
            } else if (null == user5.getPassword()) {
                return user5.getUsername().substring(0, 2).contains("06");
            }
            return "06".contains(user5.getPassword());
        }).forEach(cvrg -> {
            flag.set(true);
            return ;
        });

        System.out.println("flag: "+flag.get());
    }
}


